package mof.gov.dmdc.view.common;
/**
 *  For Project DMDC 4.0
 *  @author Nguyen Trong Cuong CMCSoft 10/09/11
 */
import java.util.Locale;
import java.util.Map;

import javax.el.ELContext;
import javax.el.ExpressionFactory;

import javax.el.ValueExpression;

import javax.faces.application.Application;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;

import oracle.adf.model.binding.DCIteratorBinding;

import oracle.adf.share.ADFContext;

import oracle.adf.view.rich.context.AdfFacesContext;

import oracle.binding.BindingContainer;

import oracle.jbo.ApplicationModule;
import oracle.jbo.LocaleContext;
import oracle.jbo.ViewObject;
import oracle.jbo.common.DefLocaleContext;

import org.apache.myfaces.trinidad.render.ExtendedRenderKitService;
import org.apache.myfaces.trinidad.util.Service;

public class Common {
    /**
     * Methor for taking a reference to a JSF (or ADF) binding expression and returning the matching object (or create it).
     * @param expression EL expression.
     * @return Managed Object.
     */
    public static Object resolveExpression(String expression) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Application appication = facesContext.getApplication();
        ExpressionFactory elFactory = appication.getExpressionFactory();
        ELContext elContext = facesContext.getELContext();
        ValueExpression valueExp = elFactory.createValueExpression(elContext, expression, Object.class);
        return valueExp.getValue(elContext);
    }

    /**
     * Return the current page's binding container.
     * @return the current page's binding container.
     */
    public static BindingContainer getBindingContainer() {
        return BindingContext.getCurrent().getCurrentBindingsEntry();
    }

    public static DCBindingContainer getDCBindingContainer() {
        return (DCBindingContainer)getBindingContainer();
    }

    /**
     * Get application module for an application module data control by name.
     * @param dcName application module data control name.
     * @return application module.
     */
    public static ApplicationModule getAppModuleForDataControl(String dcName) {
        return (ApplicationModule)resolveExpression("#{data." + dcName + ".dataProvider}");
    }

    /**
     * Get DCIterator by name
     * @param iteratorName
     * @return DCIterator
     */
    public static DCIteratorBinding findIterator(String iteratorName) {
        try {
            return getDCBindingContainer().findIteratorBinding(iteratorName);
        } catch (NullPointerException e) {
            throw new RuntimeException("Iterater '" + iteratorName + "' not found!");
        }
    }

    /**
     * Methor for Getting ViewObject by Iterater name.
     * @param iteratorName Iterator Name.
     * @return ViewObject u need.
     */
    public static ViewObject getViewObjectForIterator(String iteratorName) {
        return findIterator(iteratorName).getViewObject();
    }

    /**
     * Methor for get value from pageFlowScope
     * @param key
     * @return
     */
    public static Object getFromPageFlowScope(String key) {
        ADFContext ctx = ADFContext.getCurrent();
        Map pageFlowScope = ctx.getPageFlowScope();
        Object object = pageFlowScope.get(key);
        return object;
    }

    /**
     * Put Object to pageFlowScope
     * @param key
     * @param value
     */
    public static void putToPageFlowScope(String key, Object value) {
        ADFContext ctx = ADFContext.getCurrent();
        Map pageFlowScope = ctx.getPageFlowScope();
        pageFlowScope.put(key, value);
    }

    /**
     * Manual refresh UIComponet (Ajax)
     * @param component UIComponent
     */
    public static void refreshComponent(UIComponent component) {
        AdfFacesContext adfFacesContext = AdfFacesContext.getCurrentInstance();
        adfFacesContext.addPartialTarget(component);
    }

    /**
     * Get UIComponent by id
     * @param id
     * @return UIComponent
     */
    public static UIComponent getComponentFromRoot(String id) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        UIComponent returnComponent;
        try {
            returnComponent = facesContext.getViewRoot().findComponent(id);
        } catch (Exception e) {
            returnComponent = null;
        }
        return returnComponent;
    }

    /**
     * Get locelContext
     * @return Current locale context
     */
    public static LocaleContext getLocaleContext() {
        Locale lc = FacesContext.getCurrentInstance().getViewRoot().getLocale();
        return new DefLocaleContext(lc);
    }

    /**
     *  Manual show popup by client popupID
     */
    public static void showPopup(String clientId) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExtendedRenderKitService renderKitService = Service.getRenderKitService(facesContext, ExtendedRenderKitService.class);
        StringBuilder script = new StringBuilder();
        script.append("var popup = AdfPage.PAGE.findComponent(\"").append(clientId).append("\");").append("popup.show();");
        renderKitService.addScript(facesContext, script.toString());
    }
}
